//
//  EBMJumpClass.m
//  eBondMall
//
//  Created by apple on 2020/4/23.
//  Copyright © 2020 zhaoshijie. All rights reserved.
//

#import "EBMJumpClass.h"
#import "EBMTabBarViewController.h"
#import "EBMWebViewViewController.h"


@implementation EBMJumpClass


//  通用的跳转
+ (void)jumpInViewControllerWithOperation:(id)operation class:(Class)class param:(id)param
{
    UIViewController *operationVC ;
    
    if([operation isKindOfClass:[UIView class]])
    {
        operationVC = [operation viewController];
    }
    else if ([operation isKindOfClass:[UIViewController class]])
    {
        operationVC = operation;
    }
    
    if(operationVC)
    {
        
        UIViewController *jumpVC = [[class alloc]init];
        
        if([jumpVC isKindOfClass:[EBMBaseViewController class]])
        {
            EBMBaseViewController *baseJumpVC = (EBMBaseViewController *)jumpVC;
            baseJumpVC.parame0 = param;
        }
        
        [operationVC.navigationController pushViewController:jumpVC animated:NO];
        
        
    }
}


/// 跳转到对应板块
/// @param index 板块位置
+ (void)jumpInTabarWithIndex:(NSInteger)index
{
    
    UIWindow *keyWindow = KWindow;
    EBMTabBarViewController *tabarVC = (EBMTabBarViewController *)keyWindow.rootViewController;
    
    
    if(tabarVC.viewControllers.count<=index)
    {
        return;
    }
    
    /// 全部的模块都回到首页
    [self allNavigationPopToRoot];
    
    tabarVC.selectedIndex = index;
    
}




/// 跳转到债券--首页
/// @param type 1：首页    2:一级    3:二级
+ (void)jumpInBondHomeWithType:(NSInteger)type
{

    
    
    UIWindow *keyWindow = KWindow;
    
    EBMTabBarViewController *tabarVC = (EBMTabBarViewController *)keyWindow.rootViewController;
    
    
    if(tabarVC.viewControllers.count>1)
    {

        /// 全部的模块都回到首页
        [self allNavigationPopToRoot];
        
        tabarVC.selectedIndex = 1;
        
        UINavigationController *navaig = tabarVC.viewControllers[1];
        
//        EBMBondViewController *viewController = (EBMBondViewController *)navaig.viewControllers.firstObject;
//        viewController.jumpInIndex = type;
        
        
    }
    
}





/// 跳转到债券详情
/// @param bondId 债券id
+ (void)jumpInBondDetailWithBondId:(NSString *)bondId
{
    UIWindow *keyWindow = KWindow;
    
    EBMTabBarViewController *tabarVC = (EBMTabBarViewController *)keyWindow.rootViewController;
    
    
    if(tabarVC.viewControllers.count>0)
    {
//        EBMBondDetailViewController *bondDetailVC = [[EBMBondDetailViewController alloc]init];
//        bondDetailVC.bondId = bondId;
//        [[self gainCurrentNavigation] pushViewController:bondDetailVC animated:YES];
        
    }
}




/// 跳转到网页
/// @param urlString 网页地址
/// @param title 标题
+ (void)jumpInWebWithUrlString:(NSString *)urlString title:(NSString *)title
{

    UIWindow *keyWindow = KWindow;
    
    EBMTabBarViewController *tabarVC = (EBMTabBarViewController *)keyWindow.rootViewController;
    
    if([self isLoginPresent])
    {
        //  未登录，退出登录界面
        
        UIViewController *present = tabarVC.presentedViewController;
        
        [present dismissViewControllerAnimated:YES completion:nil];
        
    }
    

    if(tabarVC.viewControllers.count>1)
    {

        EBMWebViewViewController *viewController = [[EBMWebViewViewController alloc]initWithUrlString:urlString titleString:title];
        [[self gainCurrentNavigation] pushViewController:viewController animated:YES];
        
    }
    
}



/// 全部的模块都回到首页
+ (void)allNavigationPopToRoot
{
    UIWindow *keyWindow = KWindow;
    
    EBMTabBarViewController *tabarVC = (EBMTabBarViewController *)keyWindow.rootViewController;
    
    for (UINavigationController *navigationVC in tabarVC.viewControllers)
    {
        [navigationVC popToRootViewControllerAnimated:NO];
        
        navigationVC.topViewController.hidesBottomBarWhenPushed = NO;
    }
    
}



/// 获取当前的导航栏
+ (UINavigationController *)gainCurrentNavigation
{
    UIWindow *keyWindow = KWindow;
    
    EBMTabBarViewController *tabarVC = (EBMTabBarViewController *)keyWindow.rootViewController;
    
    UINavigationController *navig = tabarVC.viewControllers[tabarVC.selectedIndex];
    
    return navig;

}


//  判断是否模态弹出的登录界面
+ (BOOL)isLoginPresent
{
    UIWindow *keyWindow = KWindow;
    
    EBMTabBarViewController *tabarVC = (EBMTabBarViewController *)keyWindow.rootViewController;
    
    id present = tabarVC.presentedViewController;
    
    return present? YES:NO;
    
}




@end
